<template>
    <div class="major-add">
        <el-form label-width="80px" ref="form" :model="form" :rules="rules">
            <el-form-item label="课程名称" prop="name">
                <el-input v-model="form.name" placeholder="请输入课程名称" />
            </el-form-item>
            <el-form-item label="年级" prop="grade">
                <el-input-number v-model="form.grade" controls-position="right" :min="0" :max="3000"></el-input-number>
            </el-form-item>
            <el-form-item label="授课教师" prop="teacherId">
                <el-select filterable v-model="form.teacherId" placeholder="请选择教师">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="上课时间" prop="time">
                <el-select v-model="form.weekday" placeholder="请选择">
                    <el-option v-for="item in options2" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
                <el-select v-model="form.lesson" placeholder="请选择">
                    <el-option v-for="item in options3" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="时常(节)" prop="time">
                <el-input-number v-model="form.duration" controls-position="right" :min="0" :max="4"></el-input-number>
            </el-form-item>
            <el-form-item label="上课地点" prop="location">
                <el-input v-model="form.location" placeholder="请输入地点" />
            </el-form-item>
            <el-form-item label="学分" prop="credit">
                <el-input-number v-model="form.credit" controls-position="right" :min="1" :max="10"></el-input-number>
            </el-form-item>
            <el-form-item label="最大容量" prop="maxSize">
                <el-input-number v-model="form.maxSize" controls-position="right" :min="1" :max="100"></el-input-number>
            </el-form-item>
            <el-form-item label="考试地点" prop="examLocation">
                <el-input v-model="form.examLocation" placeholder="请输入考试地点" />
            </el-form-item>
            <el-form-item label="考试时间" prop="examDate">
                <el-date-picker v-model="form.examDate" type="datetime" placeholder="选择时间">
                </el-date-picker>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer" style="display: flex; justify-content: flex-end;">
            <el-button type="primary" @click="onsubmit">确定</el-button>
            <el-button type="primary" @click="handclose" plain>取消</el-button>
        </span>
    </div>
</template>
<script>
import { teachernames, admincourseid, courseup } from '@/api/courseset';
import dayjs from 'dayjs';
export default {
    props: {
        id: { type: Number, default: null }
    },
    data() {
        return {
            form: { name: '', time: "", id: "", credit: "5", teacherId: '', grade: '2019', weekday: '星期一', lesson: '第一节', duration: '2', location: '', maxSize: '50', examLocation: '', examDate: '' },
            options: [{
                value: '',
                label: ''
            }],
            options2: [{
                value: '1',
                label: '星期一'
            }, {
                value: '2',
                label: '星期二'
            }, {
                value: '3',
                label: '星期三'
            }, {
                value: '4',
                label: '星期四'
            }, {
                value: '5',
                label: '星期五'
            }, {
                value: '6',
                label: '星期六'
            }, {
                value: '7',
                label: '星期日'
            }],
            options3: [{
                value: '1',
                label: '第一节'
            }, {
                value: '2',
                label: '第二节'
            }, {
                value: '3',
                label: '第三节'
            }, {
                value: '4',
                label: '第四节'
            }, {
                value: '5',
                label: '第五节'
            }, {
                value: '6',
                label: '第六节'
            }, {
                value: '7',
                label: '第七节'
            }, {
                value: '8',
                label: '第八节'
            }, {
                value: '9',
                label: '第九节'
            }],
            rules: {
                name: [
                    { required: true, message: '请输入课程名称', trigger: 'blur' }
                ],
            },
        }
    },
    mounted() {
        teachernames().then(res => {
            console.log(res);
            this.options = (res.data || []).map(item => ({
                label: item.name,
                value: item.id
            }));
        })
        if (this.id) {
            admincourseid(this.id).then(res => {
                if (res.code === 0) {
                    console.log(res);
                    this.form = res.data;
                    if (res.data.time) {
                        const arr = res.data.time.split('-');
                        this.form.weekday = arr[0] || '';
                        this.form.lesson = arr[1] || '';
                        this.form.duration = arr[2] || '';
                    }

                } else {
                    this.$message.error('获取专业信息失败');
                }
            });
        }
    },
    methods: {
        async onsubmit() {
            this.$refs["form"].validate(async flag => {
                if (flag) {
                    this.form.time = `${this.form.weekday}-${this.form.lesson}-${this.form.duration}`;
                    console.log(this.form)
                    if (this.form.examDate) {
                        this.form.examDate = dayjs(this.form.examDate).format('YYYY-MM-DD HH:mm');
                    }
                    const res = await courseup(this.form);
                    if (res.code == 0) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                        });
                        this.$emit('done');
                    } else {
                        console.log(res);
                        this.$message({
                            message: '操作失败',
                            type: 'error',
                        });
                    }
                }
            })
        },
        handclose() {
            this.$emit('close')
        }
    }
}
</script>
